package com.nf.health.app.adapter;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.mine.activity.SchemeDetailActivity;
import com.nf.health.app.models.SchemeBean;
import com.nf.health.app.utils.ActivityUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Jason on 2016/3/31.
 */
public class HomeSchemeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int FOOTER = 0;
    private static final int ITEM = 1;
    List<SchemeBean> schemeBeans;

    private boolean isExpand;
    private TextView mExpandTextView;

    public HomeSchemeAdapter(List<SchemeBean> schemeBeans, boolean isExpand) {
        this.schemeBeans = schemeBeans;
        this.isExpand = isExpand;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == ITEM){
            View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_scheme, parent, false);
            return new SchemeViewHolder(rootView);
        }else {
            View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_scheme_footer, parent, false);
            return new FooterViewHolder(rootView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  SchemeViewHolder){
            SchemeViewHolder itemHolder = (SchemeViewHolder)holder;
            itemHolder.tvTitle.setText(schemeBeans.get(position).getName());
            itemHolder.tvContent.setText(schemeBeans.get(position).getSchemename());

            if(schemeBeans.get(position).getState().equals("1")){

            }else{
                itemHolder.btnAdd.setText("已完成");
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if((!isExpand && position == 3) ||(isExpand && position == schemeBeans.size())){
            //点击展开
            return FOOTER;
        }else{
            return ITEM;
        }
    }

    @Override
    public int getItemCount() {
        if (!isExpand) {
            if (schemeBeans.size() > 3) {
                return 3+1;
            } else {
                return schemeBeans.size();
            }
        } else {
            return schemeBeans.size()+1;
        }
    }

   public class SchemeViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.btn_add)
       TextView btnAdd;
        @Bind(R.id.btn_add_lin)
        RelativeLayout btn_add_lin;
        public SchemeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", schemeBeans.get(getPosition()).getId());
                    bundle.putString("sport_name", schemeBeans.get(getPosition()).getSchemename());
                    bundle.putString("type", schemeBeans.get(getPosition()).getState());
                    ActivityUtils.showActivity(v.getContext(), SchemeDetailActivity.class, bundle);
                }
            });
            btn_add_lin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id",schemeBeans.get(getPosition()).getId());
                    bundle.putString("sport_name",schemeBeans.get(getPosition()).getSchemename());
                    bundle.putString("type",schemeBeans.get(getPosition()).getState());
                    ActivityUtils.showActivity(v.getContext(), SchemeDetailActivity.class,bundle);
                }
            });

        }

    }
    class FooterViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_expand)
        TextView tvExpand;
        @Bind(R.id.iv_up_down)
        ImageView ivUpDown;
        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            tvExpand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isExpand){
                        tvExpand.setText("展开更多");
                        ivUpDown.setBackgroundResource(R.drawable.home_docinfo_more_down);
                    }else{
                        tvExpand.setText("点击收起");
                        ivUpDown.setBackgroundResource(R.drawable.home_docinfo_more_up);
                    }
                    isExpand = !isExpand;
                    notifyDataSetChanged();
                }
            });
        }
    }

}
